package com.yuanzong.wuliu.service.app.business.customer;

import com.yuanzong.wuliu.common.exception.BusinessException;
import com.yuanzong.wuliu.common.utils.id.SnowFlakeUtils;
import com.yuanzong.wuliu.common.vo.ApiResult;
import com.yuanzong.wuliu.common.vo.ApiResultPage;
import com.yuanzong.wuliu.controller.app.business.customer.param.AppBusinessCustomerBaodanAddParam;
import com.yuanzong.wuliu.controller.app.business.customer.param.AppBusinessCustomerBaodanEndParam;
import com.yuanzong.wuliu.controller.app.business.customer.param.AppBusinessCustomerBaodanPageParam;
import com.yuanzong.wuliu.controller.intercdeptor.AppHandlerInterceptorAdapter;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomer;
import com.yuanzong.wuliu.model.business.customer.BusinessCustomerBaodan;
import com.yuanzong.wuliu.model.business.customer.query.BusinessCustomerBaodanPageQuery;
import com.yuanzong.wuliu.service.business.customer.factory.BusinessCustomerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AppBusinessCustomerBaodanService {

    public ApiResult page(AppBusinessCustomerBaodanPageParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        BusinessCustomer runtimeUser = AppHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数验证
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        BusinessCustomerBaodanPageQuery businessCustomerBaodanPageQuery = new BusinessCustomerBaodanPageQuery();
        BeanUtils.copyProperties(param, businessCustomerBaodanPageQuery);
        businessCustomerBaodanPageQuery.setCustomerId(runtimeUser.getId());
        ApiResultPage<BusinessCustomerBaodan> apiResultPage = BusinessCustomerFactory.getBusinessCustomerBaodanService().pageSubject(businessCustomerBaodanPageQuery);
        return ApiResult.success(apiResultPage);
    }

    public ApiResult add(AppBusinessCustomerBaodanAddParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        BusinessCustomer runtimeUser = AppHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数验证
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        BusinessCustomerBaodan businessCustomerBaodan = new BusinessCustomerBaodan();
        BeanUtils.copyProperties(param, businessCustomerBaodan);
        businessCustomerBaodan.setId(SnowFlakeUtils.getId());
        businessCustomerBaodan.setCustomerId(runtimeUser.getId());
        businessCustomerBaodan.setStatus(1);
        businessCustomerBaodan.setCreateTime(LocalDateTime.now());
        BusinessCustomerFactory.getBusinessCustomerBaodanService().save(businessCustomerBaodan);
        return ApiResult.success();
    }

    public ApiResult end(AppBusinessCustomerBaodanEndParam param) throws Exception {
        // ===================================================================================//
        // ===== 当前登陆用户
        // ===================================================================================//
        BusinessCustomer runtimeUser = AppHandlerInterceptorAdapter.getRuntimeUser();
        // ===================================================================================//
        // ===== 参数验证
        // ===================================================================================//
        if (param == null) {
            throw new BusinessException("参数为空");
        }
        BusinessCustomerBaodan businessCustomerBaodan = BusinessCustomerFactory.getBusinessCustomerBaodanService().getById(param.getId());
        if (businessCustomerBaodan == null) {
            throw new BusinessException("报单不存在");
        }
        // ===================================================================================//
        // ===== 业务执行
        // ===================================================================================//
        businessCustomerBaodan.setStatus(2);
        businessCustomerBaodan.setEndTime(LocalDateTime.now());
        BusinessCustomerFactory.getBusinessCustomerBaodanService().updateById(businessCustomerBaodan);
        return ApiResult.success();
    }
}
